package com.itheima.service.impl;

import com.itheima.dao.BookDao;
import com.itheima.dao.UserDao;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

//第二步，在实现类中写注解。
//定义bean，后面添加bean的id
@Component("userService")
//设定bean的作用域
@Scope("singleton")//可以直接写@Scope
public class UserServiceImpl implements UserService {

    @Value("3")
    private int num;
    @Value("itheima ")
    private String version;

    @Autowired
    @Qualifier("userDao2")
    private UserDao userDao;

    @Autowired
    private BookDao bookDao;
//    这里不需要set，前期写set是因为私有的东西其他访问不了
//    public void setNum(int num) {
//        this.num = num;
//    }
//
//    public void setVersion(String version) {
//        this.version = version;
//    }

    public void save() {
        System.out.println("user service is running..." + num + " " + version);
        userDao.save();
        bookDao.save();
    }

//    设定bean的生命周期
    @PostConstruct
    public void init() {
        System.out.println("user service's init...");
    }
//  设定bean的生命周期
    @PreDestroy
    public void destroy() {
        System.out.println("user service's destroy...");
    }
}
